import { createSlice,createAsyncThunk } from '@reduxjs/toolkit'

export const contralTestActive = createAsyncThunk('contral/contralTestActive', async () => {
    return await new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                    testActive: 'contral vTestActive 这是异步数据测试',
                    value: 100
                }
            )
        }, 2000)
    })
})

const counterSlice = createSlice({
    //dispatch('counter/increment')
    name: 'counter',
    initialState: {
        value: 0,
        testActive: ''
    },
    reducers: {
        increment: (state,action) => {
            state.value += action.payload
        },
        changeTestActive: (state, action) => {
            state.testActive = action.payload.testActive
            state.value = action.payload.value
        }
    },
    extraReducers: (builder) => {
        builder.addCase(contralTestActive.fulfilled, (state, action) => {
            state.testActive = action.payload.testActive
            state.value = action.payload.value
        })
    }
})

export default counterSlice.reducer